/*
 * ComplexContent.java
 *
 * Created on 20 marzo 2004, 10.24
 */

package net.ermannofranco.xml.schema;

/**
 * 
 * @author ermanno (ermanno.franco@gmail.com)
 */
public class ComplexContent extends SchemaTag {

	private static final long serialVersionUID = -8274662163467123771L;
	private boolean content;

	/**
	 * Creates a new instance of ComplexContent Protetto
	 * 
	 * @param level
	 *            livello di indentazione
	 * @param container
	 *            tag contenitore
	 */
	protected ComplexContent(int level, ISchemaTag container) {
		super("complexContent", level, container);
	}

	private Extension setExtension() {
		if (content)
			throw new SchemaException(
					"Extension non permessa, contenuto gia' assegnato");
		Extension e;
		add(e = new Extension(getLevel() + 1, this));
		content = true;
		return e;
	}

	public Extension setExtension(ComplexType type) {
		return (Extension) setExtension().setBase(type);
	}

	public Extension setExtension(String typeName) {
		return (Extension) setExtension().setBase(typeName);
	}

	protected RestrictionComplexContent setRestriction() {
		RestrictionComplexContent re;
		if (content)
			throw new SchemaException(
					"Restriction non permessa, contenuto gia' assegnato");
		add(re = new RestrictionComplexContent(getLevel() + 1, this));
		content = true;
		return re;
	}

	public RestrictionComplexContent setRestriction(ContentType typeRef) {
		return setRestriction().setBase(typeRef);
	}
	public RestrictionComplexContent setRestriction(XmlType typeRef) {
		return setRestriction().setBase(typeRef);
	}
	
//	public RestrictionComplexContent setRestriction(String typeName) {
//		return setRestriction().setBase(typeName);
//	}

	// public RestrictionComplexContent setRestriction(XmlType type) {
	// return setRestriction().setBase(type);
	// }

	/**
	 * aggiunge un tag Annotation che contiene un tag Documentation
	 * 
	 * @param explanation
	 *            Stringa di documentazione
	 * @return autoriferimento all'oggetto corrente
	 */
	public ComplexContent addDocumentation(String explanation) {
		addAnnotation().addDocumentation(explanation);
		return this;
	}

	/**
	 * aggiunge un tag Annotation che contiene un tag Documentation
	 * 
	 * @param explanation
	 *            testo della documentazione
	 * @param language
	 *            attributo di xml:lang, seguire le regole di i18n
	 * @return riferimento all'oggetto corrente
	 */
	public ComplexContent addDocumentation(String explanation, String language) {
		addAnnotation().addDocumentation(explanation, language);
		return this;
	}

	/**
	 * semplice override di convalida
	 * 
	 * @return super.toString()
	 */
	public String toSave() {
		if (!content)
			throw new SchemaException("Assegnare contenuto a complexContent");
		return super.toSave();
	}

	public String toPrint() {
		if (!content)
			throw new SchemaException("Assegnare contenuto a complexContent");
		return super.toPrint();
	}
}
